/**
 * Description: SystemAnnouncementAction.java
 * Copyright (c) 2008-2009  Neo. 
 * All Rights Reserved.
 * @version 1.0  Mar 29, 2009 12:47:43 PM 李萌（neolimeng@gmail.com）created
 */
package org.paradise.dms.web.action.systemannouncement;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.pojo.SystemAnnouncement;
import org.paradise.dms.services.SystemAnnouncementService;
import org.paradise.dms.services.impl.DormitoryServiceImpl;
import org.paradise.dms.web.action.DMSBaseAction;
import org.paradise.dms.web.tools.Pager;
import org.paradise.dms.web.tools.PagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * Description: Copyright (c) 2008-2009 Neo. All Rights Reserved.
 * 
 * @version 1.0 Mar 29, 2009 12:47:43 PM 李萌（neolimeng@gmail.com）created
 */
@RemoteProxy(name = "systemAnnouncementAction")
@Service
public class SystemAnnouncementAction extends DMSBaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1515352141995965934L;
	private static Logger log = Logger.getLogger(DormitoryServiceImpl.class);
	@Autowired
	@Qualifier("systemAnnouncementServiceImpl")
	SystemAnnouncementService systemannouncementservice;
	private SystemAnnouncement sa;
	private List<SystemAnnouncement> sl;

	@Autowired
	private PagerService pagerService;
	private Pager pager;
	protected String currentPage;
	protected String totalRows;
	protected String pagerMethod;

	/**
	 * 
	 * Description: Action-插入一条公告记录
	 * 
	 * @Version1.0 Mar 29, 2009 12:56:51 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String insertSystemAnnouncement() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		String recorderId = (String) session.getAttribute("systemuserid");
		String recorderName = (String) session.getAttribute("systemusername");
		sa.setSystemannouncementposter(recorderName);
		sa.setSystemannouncementposttime(new Date());
		if (this.systemannouncementservice.insertSystemAnnouncement(sa)) {
			log.info("DMS_info_系统用户ID：" + recorderId + "用户名：" + recorderName
					+ "添加了一条公告，公告标题为：" + sa.getSystemannouncementtitle());
			return SUCCESS;
		} else
			log.error("DMS_error_系统用户ID：" + recorderId + "用户名：" + recorderName
					+ "添加了一条公告，失败！！");
		return INPUT;
	}

	/**
	 * 
	 * Description: Action-更新一条公告记录
	 * 
	 * @Version1.0 Mar 29, 2009 12:56:51 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String updateSystemAnnouncement() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		String recorderId = (String) session.getAttribute("systemuserid");
		String recorderName = (String) session.getAttribute("systemusername");
		sa.setSystemannouncementposter(recorderName);
		sa.setSystemannouncementposttime(new Date());
		if (this.systemannouncementservice.updateSystemAnnouncement(sa)) {
			log.info("DMS_info_系统用户ID：" + recorderId + "用户名：" + recorderName
					+ "更新了一条公告，公告标题为：" + sa.getSystemannouncementtitle());
			return SUCCESS;
		} else
			log.error("DMS_error_系统用户ID：" + recorderId + "用户名：" + recorderName
					+ "更新了一条公告，失败！！");
		return INPUT;
	}

	/**
	 * 
	 * Description: Action-删除一条公告记录
	 * 
	 * @Version1.0 Mar 29, 2009 12:56:51 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String deleteSystemAnnouncement() {
		int said = Integer.parseInt(ServletActionContext.getRequest()
				.getParameter("systemannouncementid"));
		if (this.systemannouncementservice.deleteSystemAnnouncement(said)) {
			return SUCCESS;
		} else
			return INPUT;
	}

	/**
	 * 
	 * Description: Action-根据公告ID取得一条公告记录
	 * 
	 * @Version1.0 Mar 29, 2009 12:56:51 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String getSystemAnnouncementBySystemAnnouncementId() {
		try {
			int said = Integer.parseInt(ServletActionContext.getRequest()
					.getParameter("systemannouncementid"));
			sa = this.systemannouncementservice.getSystemAnnouncement(said);
			ServletActionContext.getRequest().getSession().setAttribute(
					"sacontent", sa.getSystemannouncementcontent().toString());
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 
	 * Description: Action-取得全部公告信息
	 * 
	 * @Version1.0 Mar 29, 2009 10:16:36 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String getAllSystemAnnouncement() {
		try {
			sl = this.systemannouncementservice.getAllSystemAnnouncement();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String[] showAllSystemAnnouncement() {
		try {
			sl = this.systemannouncementservice.getAllSystemAnnouncement();
			if (sl == null || sl.size() == 0) {
				System.out.println("0000000000000000000");
				return null;
			} else {
				String[] sa = new String[sl.size()];
				for (int i = 0; i < sl.size(); i++) {
					sa[i] = "<a href=\"/dms/systemannouncement/getsystemannouncementbyid.action?systemannouncementid="
							+ sl.get(i).getSystemannouncementid()
							+ "\">"
							+ sl.get(i).getSystemannouncementtitle() + "</a>";
				}
				return sa;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String listAllSystemAnnouncement() {
		try {
			int totalRow = this.systemannouncementservice.getAllRows();
			pager = pagerService.getPager(this.getCurrentPage(), this
					.getPagerMethod(), totalRow);
			this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
			this.setTotalRows(String.valueOf(totalRow));
			sl = this.systemannouncementservice.listAllSystemAnnouncement(pager
					.getPageSize(), pager.getStartRow());
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * @return the systemannouncementservice
	 */
	public SystemAnnouncementService getSystemannouncementservice() {
		return systemannouncementservice;
	}

	/**
	 * @param systemannouncementservice
	 *            the systemannouncementservice to set
	 */
	public void setSystemannouncementservice(
			SystemAnnouncementService systemannouncementservice) {
		this.systemannouncementservice = systemannouncementservice;
	}

	/**
	 * @return the sa
	 */
	public SystemAnnouncement getSa() {
		return sa;
	}

	/**
	 * @param sa
	 *            the sa to set
	 */
	public void setSa(SystemAnnouncement sa) {
		this.sa = sa;
	}

	/**
	 * @return the sl
	 */
	public List<SystemAnnouncement> getSl() {
		return sl;
	}

	/**
	 * @param sl
	 *            the sl to set
	 */
	public void setSl(List<SystemAnnouncement> sl) {
		this.sl = sl;
	}

	public PagerService getPagerService() {
		return pagerService;
	}

	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(String totalRows) {
		this.totalRows = totalRows;
	}

	public String getPagerMethod() {
		return pagerMethod;
	}

	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}

}
